/*
 * ChoosePayWayFragment.java
 * business
 *
 * Created by chentao on 2017/5/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.choosepayway;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.ShopPayWays;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.ui.widget.recyclerview.provider.ChoosePayWayProvider;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 选择支付方式-View
 */
public class ChoosePayWayFragment extends SuperBaseFragment implements ChoosePayWayContract.View {

    private ChoosePayWayContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private RecyclerView mRecyclerView;

    private Button mBtnConfirm;

    private MultiTypeAdapter mAdapter;

    public static ChoosePayWayFragment getInstance() {
        ChoosePayWayFragment fragment = new ChoosePayWayFragment();
        return fragment;
    }

    @Override
    public void setPresenter(ChoosePayWayContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ChoosePayWayFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mBtnConfirm = (Button) mRootView.findViewById(R.id.btn_confirm);

        initRecyclerView();
        initListeners();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });

        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  确定支付方式
                mPresenter.confirmPayWays();
            }
        });
    }

    private void initRecyclerView() {
        mAdapter = new MultiTypeAdapter();
        ChoosePayWayProvider choosePayWayProvider = new ChoosePayWayProvider(mActivity, new ChoosePayWayProvider.OnTagClickListener() {
            @Override
            public boolean onTagClick(ShopPayWays shopPayWays, PayWay payWay) {
                return mPresenter.checkOnlinePayLimit(shopPayWays, payWay);
            }
        });
        mAdapter.register(ShopPayWays.class, choosePayWayProvider);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void setItems(List<?> items) {
        mAdapter.setItems(items);
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_pay_way;
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }
}
